#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "sqlconsole.h"
#include "changeuserdialog.h"

#include "streetsdict.h"
#include "roadsdict.h"
#include "typestrdict.h"
#include "typesdtpdict.h"
#include "distrofcitydict.h"
#include "useradmindict.h"
#include "usersettingsdialog.h"
#include "diagramaffectdeaddialog.h"
#include "dialogaddiagram.h"

#include "logic.h"
#include <QCloseEvent>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow)
{
    m_ui->setupUi(this);
    setWindowTitle(tr("Прорамма анализа аварийности по Ставропольскому краю"));
}

MainWindow::~MainWindow()
{
    delete m_ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_actAboutProgram_triggered()
{
      QMessageBox::about(this, tr("О программе"),
                       tr("Тут будет умная инфа!"));
}

void MainWindow::on_actAboutQt_triggered()
{
    qApp->aboutQt();
}

void MainWindow::on_actSqlConsole_triggered()
{
    SqlConsole console;
    console.exec();
}

void MainWindow::on_actDictStreets_triggered()
{
    StreetsDict *streetsDict = new StreetsDict();
    streetsDict->exec();
}

void MainWindow::on_actDictRoads_triggered()
{
    RoadsDict *roadsDict = new RoadsDict();
    roadsDict->exec();
}


void MainWindow::on_actDictDistricts_triggered()
{
    DistrOfCityDict *distrDict = new DistrOfCityDict();
    distrDict->exec();
}


void MainWindow::on_actDictTypeOfDTP_triggered()
{
    TypesDtpDict *typesDtpDict = new TypesDtpDict();
    typesDtpDict->exec();
}


void MainWindow::on_actDictTypeOfTV_triggered()
{
    TypesTrDict *typesTrDict = new TypesTrDict();
    typesTrDict->exec();
}

void MainWindow::on_actChangeUser_triggered()
{
    changeUserDialog.exec();
    emit userChanged();
}

void MainWindow::on_actUserControl_triggered()
{
    UserAdminDict userControl;
    userControl.exec();
}

void MainWindow::on_actExit_triggered()
{
    this->close();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (QMessageBox::question(this, tr("Подтверждение закрытия программы"),
                          tr("Вы действительно хотите завершить программу?"),
                          QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
        event->accept();
    }
    else {
        event->ignore();
    }
}

void MainWindow::setPermissionsOnGui()
{
    int permissions = logic::permissions();
    if (permissions < 777){
        m_ui->actBackupDatabase->setDisabled(true);
        m_ui->actDbSetup->setDisabled(true);
        m_ui->actUserControl->setDisabled(true);
        m_ui->actUserMonitor->setDisabled(true);
    }
    else {
        m_ui->actBackupDatabase->setDisabled(false);
        m_ui->actDbSetup->setDisabled(false);
        m_ui->actUserControl->setDisabled(false);
        m_ui->actUserMonitor->setDisabled(false);
    }

}

void MainWindow::on_actPreferences_triggered()
{
    UserSettingsDialog dialog;
    dialog.exec();
}

void MainWindow::on_actDiagramAffect_triggered()
{
    DiagramAffectDeadDialog dialog;
    dialog.exec();
}

void MainWindow::on_actDiagramAffect2_triggered()
{
    DialogADDiagram dialog;
    dialog.exec();
}
